require.config({
    baseUrl:"module",
    paths:{
        jq:"../libs/jquery"
    }
})

require(["jq","Fd",'utils'],(_,Fd,utils)=>{
    let goodId = utils.getQueryParam('goodId')
    let detail = {}
    //获取商品详情
    $.ajax({
        url:"http://127.0.0.1:3000/apiData/detail.json?goodId="+goodId,
        type:"get",
        dataType:'json',
        success:(res)=>{
            console.log(res);
            detail = res
            renderDetail(res)
        }
    })


    //点击加入购物车
    $("#addcart").click(function(){
        // console.log(1);
        let bddata =localStorage.getItem("addcart")? JSON.parse(localStorage.getItem("addcart")):[];
        bddata.push({
            "name":detail.title,
            "url":detail.smImg ,
            "price":detail.price,
            "num":1,
            "proId":detail.goodId
        })
        localStorage.setItem("addcart",JSON.stringify(bddata))
        location.href = "cart.html"
    })









    new Fd({
        sBox: $(".smallImg")[0],
        bBox: $(".bigImg")[0],
        span: $(".smallImg span")[0],
        bImg: $(".bigImg img")[0],
    });
})

function renderDetail(data){
    $('.product-title').html(renderNavPath(data.navPath))
    $('.smallImg>img').attr('src',data.smImg)
    $('.bigImg>img').attr('src',data.bigImg)
    $('.proact .title').html(data.title)
    $('.proact .goodRatio').html(data.goodRatio+'%')
    $('.subTitle').html(data.subTitle)
    $('.realPrice').html("低至￥"+ data.realPrice)
    $('.pro-right-price').html("￥"+ data.price)
}

function renderNavPath(navList){
    let html = '<span>首页></span> '
    navList.forEach((item,index) => {
        html+= `<span><a href="">${item}${index<navList.length-1?'> ':' '}</a></span>`
    });
    return html
}